package com.kaiwukj.android.ufamily.mvp.ui.page.account.login;

import android.os.Bundle;
import android.widget.Button;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.kaiwukj.android.ufamily.R;
import com.kaiwukj.android.ufamily.app.base.review.BaseFragment;
import com.kaiwukj.android.ufamily.mvp.ui.widget.CodeEditText;

import java.util.concurrent.TimeUnit;

import butterknife.BindView;
import io.reactivex.Observable;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;

public class SendVerifyCodeFragment extends BaseFragment {

    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.tv_msg)
    TextView tvMsg;
    @BindView(R.id.et_code)
    CodeEditText etCode;
    @BindView(R.id.btn_reply)
    Button btnReply;

    String mobile;

    private OnVerifyCodeSendListener onVerifyCodeSendListener;


    public static SendVerifyCodeFragment newInstance(String mobile) {
        Bundle bundle = new Bundle();
        bundle.putString("mobile", mobile);
        SendVerifyCodeFragment fragment
                = new SendVerifyCodeFragment();
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bundle bundle = getArguments();
        if (bundle != null) {
            mobile = bundle.getString("mobile");
        }
    }

    public void setOnVerifyCodeSendListener(OnVerifyCodeSendListener listener) {
        onVerifyCodeSendListener = listener;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_verify_code_send;
    }

    @Override
    protected void initView() {
        etCode.requestFocus();
        tvMsg.setText(String.format("已向 %s 发送了验证码", mobile));
        btnReply.setEnabled(false);

        startTimer();

        btnReply.setOnClickListener(v -> {
            if (onVerifyCodeSendListener != null) {
                onVerifyCodeSendListener.onReply(mobile);
                startTimer();
            }
        });

        etCode.setOnTextFinishListener((text, length) -> {
            if (onVerifyCodeSendListener != null) {
                onVerifyCodeSendListener.onVerify(mobile, text.toString().trim());
            }
        });
    }

    @Override
    protected void initData(Bundle savedInstanceState) {

    }

    private void startTimer() {
        Observable.interval(0, 1, TimeUnit.SECONDS)
                .take(60)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<Long>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        subscribe(d);
                    }

                    @Override
                    public void onNext(Long aLong) {
                        btnReply.setText(String.format("%sS后重新发送", 59 - aLong));
                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onComplete() {
                        btnReply.setText("重新发送");
                        btnReply.setEnabled(true);
                    }
                });
    }

    public void notifyFiled() {

    }

    public interface OnVerifyCodeSendListener {

        void onVerify(String mobile, String code);

        void onReply(String mobile);
    }
}
